// SPDX-License-Identifier: GPL-3.0-or-later

import QtQuick
import QtQuick.Layouts

import Fk

Item {
  id: root
  property alias skill_buttons: skill_buttons
  height: 22
  width: skills.count * 57

  clip: false


  ListModel {
    id: skills
  }

  Row {
    id: buttonFlow

    Repeater {
      id: skill_buttons
      model: skills
      onItemAdded: parent.forceLayout()
      SkillButton {
        skill: model.skill
        type: "active"
        enabled: false
        orig: model.orig_skill
        forceShort: true

        onPressedChanged: {
          if (enabled)
          roomScene.activateSkill(orig, pressed, "click");
        }

      }
    }
  }

  function addSkill(skill_name, prelight) {
    const modelContains = (m, e) => {
      for (let i = 0; i < m.count; i++) {
        if (m.get(i).orig_skill === e.orig_skill) {
          return true;
        }
      }
      return false;
    };

    const data = Lua.call("GetSkillData", skill_name);

    if (!modelContains(skills, data))
      skills.append(data);
  }

  function loseSkill(skill_name, prelight) {

    for (let i = 0; i < skills.count; i++) {
      const item = skills.get(i);
      if (item.orig_skill == skill_name) {
        skills.remove(i);
      }
    }
  }

  function clearSkills() {
    skills.clear();
  }
}
